package com.bob.iodemo;

import org.hellojavaer.poi.excel.utils.ExcelType;
import org.hellojavaer.poi.excel.utils.ExcelUtils;
import org.hellojavaer.poi.excel.utils.write.ExcelWriteContext;
import org.hellojavaer.poi.excel.utils.write.ExcelWriteException;
import org.hellojavaer.poi.excel.utils.write.ExcelWriteFieldMapping;
import org.hellojavaer.poi.excel.utils.write.ExcelWriteSheetProcessor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class Demo {

    @RequestMapping("test")
    public void test(HttpServletRequest request, HttpServletResponse response) {
        ExcelWriteSheetProcessor processor = new ExcelWriteSheetProcessor() {
            @Override
            public void beforeProcess(ExcelWriteContext excelWriteContext) {

            }

            @Override
            public void onException(ExcelWriteContext excelWriteContext, ExcelWriteException e) {

            }

            @Override
            public void afterProcess(ExcelWriteContext excelWriteContext) {

            }
        };

        ExcelWriteFieldMapping fieldMapping = new ExcelWriteFieldMapping();
        fieldMapping.put("A", "className").setHead("班级");
        fieldMapping.put("B", "timeFrom").setHead("开始时间");
        fieldMapping.put("C", "timeTo").setHead("结束时间");
        fieldMapping.put("D", "planClassTime").setHead("时长");
        fieldMapping.put("E", "classType").setHead("类型");
        fieldMapping.put("F", "subject").setHead("科目");
        fieldMapping.put("G", "clock").setHead("上课情况");
        String fileName = "文件测试管理";

        processor.setSheetIndex(0);// required. It can be replaced with 'setSheetName(sheetName)';
        processor.setSheetName(fileName);
        processor.setStartRowIndex(1);//
        processor.setFieldMapping(fieldMapping);// required
        processor.setHeadRowIndex(0);

        List<Map> mapList = new ArrayList<>();
        for(int i = 0; i <= 10; i ++) {
            Map item = new HashMap();
            item.put("className", "1");
            item.put("timeFrom", "1");
            item.put("timeTo", "1");
            item.put("planClassTime", "1");
            item.put("classType", "1");
            item.put("subject", "1");
            item.put("clock", "1");
            mapList.add(item);
        }

        processor.setDataList(mapList);
        try {

            response.setContentType("application/x-msdownload");

            response.setHeader("Content-Disposition", "attachment;filename=\"" + URLEncoder.encode(fileName,"utf-8") + ".xlsx\"");
            OutputStream outputStream = response.getOutputStream();

            ExcelUtils.write(ExcelType.XLSX, outputStream, processor);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
